package com.zhangxiaolong.service.impl;

import com.zhangxiaolong.mapper.DeptMapper;
import com.zhangxiaolong.mapper.EmpMapper;
import com.zhangxiaolong.pojo.Dept;
import com.zhangxiaolong.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Integer id) {
        deptMapper.deleteById(id); //根据id删除部门数据

        empMapper.deleteByDeptId(id); //根据部门id删除该部门下的员工

    }
    @Override
    public void add(Dept dept) {
       dept.setCreateTime(LocalDateTime.now());
       dept.setUpdateTime(LocalDateTime.now());
       deptMapper.add(dept);
    }

    @Override
    public Dept findById(Integer id) {
        Dept dept = deptMapper.findById(id);
        return dept;
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
